import 'package:data/repository/home_articles/home_articles_repository.dart';
import 'package:data/response/article_response.dart';
import 'package:domain/usecases/home_article_use_case.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';

class MockRepo extends Mock implements HomeArticlesRepository {}

void main() {
  group('test home article use case', () {
    late MockRepo mockRepo;
    late HomeArticleUseCase useCase;

    setUp(() {
      mockRepo = MockRepo();
      useCase = HomeArticleUseCaseImpl(repository: mockRepo);
    });

    tearDown(() {
      reset(mockRepo);
    });

    test('should has result', () async {
      final categoryId = 1;
      final categoryName = "ss";
      final title = "sfafs";
      final author = "fdfdaf";
      final date = "sss";
      final id = 111;
      final response = ArticleResponse(
        id: id,
        superChapterName: categoryName,
        superChapterId: categoryId,
        title: title,
        author: author,
        niceDate: date,
      );
      when(
        () => mockRepo.fetchHomeArticles(page: any(named: 'page')),
      ).thenAnswer((_) async => [response]);

      final pageIndex = 22;
      final result = await useCase.fetchArticles(page: pageIndex);

      final captured = verify(
        () => mockRepo.fetchHomeArticles(page: captureAny(named: 'page')),
      ).captured;

      expect(captured[0], pageIndex);
      expect(result.length, 1);
      final model = result[0];
      expect(model.author, author);
      expect(model.title, title);
      expect(model.categoryId, categoryId.toString());
      expect(model.category, categoryName);
      expect(model.publishDate, date);
      expect(model.id, id.toString());
    });
    test('should not has result', () async {
      when(
        () => mockRepo.fetchHomeArticles(page: any(named: 'page')),
      ).thenAnswer((_) async => []);
      final result = await useCase.fetchArticles(page: 2);
      expect(result.isEmpty, true);
    });
  });
}
